package com.edmond.jyframe.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.edmond.jyframe.presenter.base.BasePresenter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;

/**
 * Created by edmond on 17-4-25.
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment {
    private P presenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ParameterizedType parameterizedType = (ParameterizedType)this.getClass().getGenericSuperclass();
        if(parameterizedType.getActualTypeArguments().length>0){
            Class<P> tClass= (Class<P>)(parameterizedType.getActualTypeArguments()[0]);
            try {
                presenter = tClass.getConstructor(this.getClass(),Class[].class).newInstance(this, initModelName());
            } catch (java.lang.InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public P getPresenter() {
        return presenter;
    }

    public abstract Class[] initModelName();
}
